#pragma once

#include "task/Task.hpp"

#include <memory>

namespace threadpool {

    class TaskSchedulerStrategy {
    public:
        virtual bool operator()(const std::shared_ptr<Task> &, const std::shared_ptr<Task> &) const = 0;
    };

    /**
      * @brief  默认的任务调度顺序策略
      * @note   定时任务 > 周期任务 > 自杀任务 > 优先级任务
      * @note   如果任务类型相同，则按任务优先级从大到小调度
      */
    class DefaultTaskSchedulerStrategy : public TaskSchedulerStrategy {
    public:
        bool operator()(const std::shared_ptr<Task> &, const std::shared_ptr<Task> &) const override;
    };

    bool DefaultTaskSchedulerStrategy::operator()(const std::shared_ptr<Task> &left,
                                                  const std::shared_ptr<Task> &right) const {
        if (left->getType() == "TimedTask") {
            if (right->getType() != "TimedTask") {
                return false;
            } else {
                return left->getPriority() < right->getPriority();
            }
        } else if (right->getType() == "TimedTask") {
            return true;
        }
        if (left->getType() == "PeriodicTask") {
            if (right->getType() != "PeriodicTask") {
                return false;
            } else {
                return left->getPriority() < right->getPriority();
            }
        } else if (right->getType() == "PeriodicTask") {
            return true;
        }
        if (left->getType() == "SuicideTask") {
            if (right->getType() != "SuicideTask") {
                return false;
            } else {
                return left->getPriority() < right->getPriority();
            }
        } else if (right->getType() == "SuicideTask") {
            return true;
        }
        return left->getPriority() < right->getPriority();
    }

}